<template>
  <view v-if="goods_info.goods_id">
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, i) in goods_info.pics" :key="i">
        <image :src="item.pics_big" @click="preview(i)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <view class="goods_price">¥{{goods_info.goods_price}}</view>
      <view class="goods">
        <view class="goods_name">{{goods_info.goods_name}}</view>

        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>

      <view class="express">快递：免运费</view>
      <!-- <view class=""></view> -->
    </view>

    <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    <!-- 商品导航组件区域 -->
    <view class="goods-nav">
      <!--  <uni-section type="line"> -->
      <uni-goods-nav :fill="true" :options="options" :button-group="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
      <!--  </uni-section> -->
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    computed: {
      // 调用 mapState 方法，把 m_cart 模块中的 cart 数组映射到当前页面中，作为计算属性来使用
      // ...mapState('模块的名称', ['要映射的数据名称1', '要映射的数据名称2'])
      ...mapState('m_cart', []),
      ...mapGetters('m_cart', ['total'])
    },
    watch: {
      total: {
        handler(newVal, oldVal) {
          const findResult = this.options.find(item => item.text === '购物车')
          // findResult为购物车
          if (findResult) {
            findResult.info = newVal
          }
        },
        immediate: true
      }
    },
    data() {
      return {
        // 商品详情对象
        goods_info: {},
        options: [{
          icon: 'chat',
          text: '客服'
        }, {
          icon: 'shop',
          text: '店铺',
          // info: 2,
          infoBackgroundColor: '#007aff',
          infoColor: "#f5f5f5"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
            color: '#fff'
          }
        ],
        customButtonGroup: [{
            text: '加入购物车',
            backgroundColor: 'linear-gradient(90deg, #1E83FF, #0053B8)',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: 'linear-gradient(90deg, #60F3FF, #088FEB)',
            color: '#fff'
          }
        ],
        customButtonGroup1: [{
          text: '立即购买',
          backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
          color: '#fff'
        }]
      }
    },
    onLoad(options) {
      const goods_id = options.goods_id || ''
      console.log('options-----', options);
      this.getgoodsDetail(goods_id)
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      async getgoodsDetail(goods_id) {
        const res = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id: goods_id
        })
        console.log('商品详情数据加载------', res);
        if (res.data.meta.status !== 200) uni.$showMsg()
        // 使用字符串的 replace() 方法，为 img 标签添加行内的 style 样式，从而解决图片底部空白间隙的问题
        res.data.message.goods_introduce = res.data.message.goods_introduce.replace(/<img /g,
          '<img style="display:block;" ').replace(/webp/g, 'jpg')
        this.goods_info = res.data.message
      },
      preview(i) {
        // 调用 uni.previewImage() 方法预览图片
        uni.previewImage({
          // 预览时，默认显示图片的索引
          current: i,
          // 所有图片 url 地址的数组
          urls: this.goods_info.pics.map(item => item.pics_big)
        })
      },
      onClick(e) {
        console.log('跳转到购物页面----', e);
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      buttonClick(e) {
        if (e.content.text === '加入购物车') {
          const goods = {
            // { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
            goods_id: this.goods_info.goods_id,
            goods_name: this.goods_info.goods_name,
            goods_price: this.goods_info.goods_price,
            goods_count: 1, // 商品的数量
            goods_small_logo: this.goods_info.goods_small_logo,
            goods_state: true
          }
          // 3. 通过 this 调用映射过来的 addToCart 方法，把商品信息对象存储到购物车中 goods传过去
          this.addToCart(goods)
        }
      },
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-info-box {
    padding: 10px 5px;

    .goods_price {
      color: red;
      font-size: 18px;
      margin: 10px 0;
    }

    .goods {
      display: flex;
      justify-content: space-between;

      .goods_name {
        // width: 85%;
        font-size: 13px;
        padding-right: 10px;
      }

      .favi {
        width: 120px;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        border-left: 2px solid #efefef;
        color: gray;
      }
    }

    .express {
      margin: 10px 0;
      font-size: 12px;
      color: gray;
    }
  }

  .goods-nav {
    position: sticky;
    bottom: 0px;
    z-index: 999;
  }
</style>
